//
//  ADUTabBar.m
//  微博OC-01
//
//  Created by lanouhn on 16/6/23.
//  Copyright © 2016年 lanouhn. All rights reserved.
//

#import "ADUTabBar.h"


@interface ADUTabBar ()

@property (nonatomic,weak) UIButton *plusBtn;
@end
@implementation ADUTabBar



//  添加加号 按钮 在初始化方法中init

// 通过代码创建控件时会调用
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
        
    }
    return self;
}

// 通过xib/Storboard创建时调用
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    // 创建一个加号按钮, 添加到当前控件中  按钮分状态设置
    /**
     有个按钮还需要设置frame button在中间最好
     */
    // 1.创建按钮
    UIButton *plusBtn = [[UIButton alloc] init];
    plusBtn.tag = 998;
    // 2.设置背景图片
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    // 3.设置图标
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    // 4.添加
    [self addSubview:plusBtn];
    
    self.plusBtn = plusBtn;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    
    NSInteger index = 0;
    for (UIView *child in self.subviews) {
        //1.判断是否是UITabBarButton
        //UITabBarButton是系统私有的控件 不能修改 找他的父控件 UIControl打印出来可以知道
        //        if ([child isKindOfClass:[UIControl class]] && ![child isKindOfClass:[UIButton class]]) {
        //               }

        if ([child isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            //计算每个控件的frame
            CGFloat childW= self.frame.size.width / 5;
            CGFloat childH = self.frame.size.height;
            CGFloat childX = index *childW;
            CGFloat childY = 0;
            //判断是否值中间一个按钮
            if (index == 2) {
                index++;
                childX = index*childW;
            }
            index++;
            child.frame = CGRectMake(childX, childY, childW, childH);
        }
        
    }
        // 设置加号按钮的frame
    //    CGPoint centerPoint = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    //    self.plusBtn.center = centerPoint;
    //    self.plusBtn.frame
    //给已经存在控件添加方法用分类 self Button都继承自UIView
    self.plusBtn.size = self.plusBtn.currentBackgroundImage.size;
    self.plusBtn.centerX = self.width * 0.5;
    self.plusBtn.centerY = self.height * 0.5;
    
    
}
@end
